import hashlib
import time
import requests
import json
import execjs

def getTimeStamp():
    node = execjs.get()
    with open('./temp/01.js', encoding='utf-8') as f:
        js_code = f.read()
    ctx = node.compile(js_code, cwd=r'./temp/node_modules')
    sign = ctx.call("getTime")
    return sign

class QianQu:
    def __init__(self, utoken, uid):
        # copy Fssetch请求
        headers = {
            "method": "POST",
            "authority": "app.szyqg.cloud",
            "scheme": "https",
            "path": "/api/index.php?c=index&a=nongchang&do=getgamedetail",
            "pragma": "no-cache",
            "cache-control": "no-cache",
            "sec-ch-ua": '"Google Chrome";v="113", "Chromium";v="113", "Not-A.Brand";v="24"',
            "sec-ch-ua-platform": '"Android"',
            "sec-ch-ua-mobile": "?1",
            "user-agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Mobile Safari/537.36",
            "content-type": "application/x-www-form-urlencoded",
            "accept": "*/*",
            "origin": "https://qg.app.blwapp.com",
            "sec-fetch-site": "cross-site",
            "sec-fetch-mode": "cors",
            "sec-fetch-dest": "empty",
            "referer": "https://qg.app.blwapp.com/",
            "accept-encoding": "gzip, deflate, br",
            "accept-language": "zh-CN,zh;q=0.9"
        }
        self.headers = headers
        self.uid = uid
        self.utoken = utoken
        self.apiKey = 'qugoushenghuo20230405..'
        self.url = "indexnongchanggetgamedetail"
        self.session = requests.session()
        self.session.headers = self.headers
        self.getGameDetail()

    def getGameDetail(self):
        # 处理时间戳
        timestamp = getTimeStamp()
        # timestamp = 1686147753530
        r = f"{self.apiKey}{timestamp}{self.uid}{self.utoken}{self.url}"
        print(r,12312)
        sign = hashlib.md5(r.encode('utf-8')).hexdigest()
        # 模拟发送请求
        url = "https://app.szyqg.cloud/api/index.php?c=index&a=nongchang&do=getgamedetail"
        data = {
            'loading': '',
            'timestamp': timestamp,
            'uid': self.uid,
            'utoken': self.utoken,
            'Sign': sign,
        }

        print(data)
        data = json.dumps(data)
        self.session.headers["Authortoken"] = sign
        response = self.session.post(url, data=data).json()
        print(response,123)


if __name__ == '__main__':
    utoken = "8cc573319ea19d4e932b052b8382f843"
    uid = '104031'
    QianQu(utoken, uid)
